<template>
  <div class="city-manage">
    <ContentField>
      <template v-slot:header> 城市管理 </template>
      <template v-slot:body>
        <div>
          <el-select
            v-model="curProvId"
            filterable
            placeholder="请选择省份"
            style="margin-right: 20px"
          >
            <el-option
              v-for="province in provinces"
              :key="province.id"
              :label="province.name"
              :value="province.id"
            >
            </el-option>
          </el-select>
          <el-button
            icon="el-icon-search"
            circle
            @click="getCitiesByPid"
          ></el-button>
          <el-button
            type="success"
            plain
            @click="addNewCityBtnClick"
            style="float: right"
          >
            添加城市
          </el-button>
        </div>
        <el-divider></el-divider>
        <div>
          <el-table :data="cities" height="500" border style="width: 100%">
            <el-table-column prop="id" label="编号" width="180">
            </el-table-column>
            <el-table-column prop="name" label="名称"> </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="editCityBtnClick(scope.row)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-dialog title="城市信息" :visible.sync="dialogFormVisible">
            <el-form :model="curCityInfo">
              <el-form-item label="所属省份" :label-width="formLabelWidth">
                <el-select
                  v-model="curCityInfo.pid"
                  filterable
                  placeholder="请选择省份"
                >
                  <el-option
                    v-for="province in provinces"
                    :key="province.id"
                    :label="province.name"
                    :value="province.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="城市名称" :label-width="formLabelWidth">
                <el-input
                  v-model="curCityInfo.name"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="danger" @click="deleteEvent">删除</el-button>
              <el-button type="primary" @click="confirmEvent">确 定</el-button>
            </div>
          </el-dialog>
        </div>
      </template>
    </ContentField>
  </div>
</template>

<script>
import ContentField from "../../components/ContentField.vue";
export default {
  name: "CityManageView",
  components: {
    ContentField,
  },
  created() {
    this.getProvinces();
  },
  methods: {
    handleClick() {},
    getProvinces() {
      let outer = this;
      this.$store.dispatch("getProvinces", {
        token: this.$store.state.user.jwt_token,
        success(resp) {
          outer.provinces = resp.data.data;
        },
        error(resp) {
          console.log(resp);
        },
      });
    },
    // 选择省份后，自动调用，获取对应的城市
    getCitiesByPid() {
      let outer = this;
      this.$store.dispatch("getCitiesByPid", {
        pid: this.curProvId,
        token: this.$store.state.user.jwt_token,
        success(resp) {
          outer.cities = resp.data.data;
        },
        error(resp) {
          console.log(resp);
        },
      });
    },
    // 编辑城市信息
    editCityBtnClick(row) {
      this.curCityInfo = row;
      this.dialogFormVisible = true;
      this.optionFlag = 1;
    },
    // 增添新的城市
    addNewCityBtnClick() {
      this.curCityInfo = {};
      this.dialogFormVisible = true;
      this.optionFlag = 2;
    },
    // 确认事件
    confirmEvent() {
      let outer = this;
      if (this.optionFlag == 1) {
        this.$store.dispatch("updateCity", {
          id: this.curCityInfo.id,
          pid: this.curCityInfo.pid,
          name: this.curCityInfo.name,
          token: this.$store.state.user.jwt_token,
          success(resp) {
            if (resp.data.code == 200) {
              outer.$message({
                type: "success",
                message: "修改城市成功",
              });
              outer.dialogFormVisible = false;
              outer.curProvId = outer.curCityInfo.pid;
              outer.getCitiesByPid();
            } else {
              outer.$message({
                type: "error",
                message: "修改城市失败：" + outer.data.message,
              });
            }
          },
          error(resp) {
            console.log(resp);
          },
        });
      } else if (this.optionFlag == 2) {
        this.$store.dispatch("addNewCity", {
          pid: this.curCityInfo.pid,
          name: this.curCityInfo.name,
          token: this.$store.state.user.jwt_token,
          success(resp) {
            if (resp.data.code == 200) {
              outer.$message({
                type: "success",
                message: "添加城市成功",
              });
              outer.dialogFormVisible = false;
              outer.curProvId = outer.curCityInfo.pid;
              outer.getCitiesByPid();
            } else {
              outer.$message({
                type: "error",
                message: "添加城市失败：" + outer.data.message,
              });
            }
          },
          error(resp) {
            console.log(resp);
          },
        });
      }
    },
    // 删除
    deleteEvent() {
      let outer = this;
      this.$store.dispatch("deleteCityById", {
        id: this.curCityInfo.id,
        token: this.$store.state.user.jwt_token,
        success(resp) {
          if (resp.data.code == 200) {
            outer.$message({
              type: "success",
              message: "删除城市成功",
            });
            outer.dialogFormVisible = false;
            outer.curProvId = outer.curCityInfo.pid;
            outer.getCitiesByPid();
          } else {
            outer.$message({
              type: "error",
              message: "删除城市失败" + resp.data.message,
            });
          }
        },
        error(resp) {
          console.log(resp);
        },
      });
    },
  },
  data() {
    return {
      curProvId: "",
      provinces: [],
      cities: [],
      dialogFormVisible: false,
      curCityInfo: {
        id: "",
        pid: "",
        name: "",
      },
      formLabelWidth: "120px",
      optionFlag: 0,
    };
  },
};
</script>

<style scoped></style>
